<template>
	<!-- 入职材料 -->
	<view>
		<form action="">
			<!-- 真实姓名 -->
			<view class="info_item">
				<text class="head_text">真实姓名</text>
				<input placeholder="请输入真实姓名" v-model="editFormData.name" style="margin-top: 39rpx;"
					placeholder-style="font-size:30rpx" />
			</view>

			<!-- 身份证号 -->
			<view class="info_item">
				<text class="head_text">身份证号</text>
				<input placeholder="请输入身份证号" v-model="editFormData.id_number" style="margin-top: 39rpx;"
					placeholder-style="font-size:30rpx" />
			</view>
			<!-- 银行卡号 -->
			<view class="info_item">
				<text class="head_text">银行卡号</text>
				<input placeholder="请输入招商银行卡号" v-model="editFormData.bank_number" style="margin-top: 39rpx;"
					placeholder-style="font-size:30rpx" />
			</view>

			<!-- 身份证人像面 -->
			<view class="info_item">
				<text class="head_text">上传身份证人像面照片</text>
				<view class="" style="margin-top: 40rpx;">
					<u-upload :fileList="fileList1" @afterRead="afterRead1" @delete="deletePic1" name="1"
						:maxCount="1"></u-upload>
				</view>
			</view>
			
			<!-- 身份证国徽面 -->
			<view class="info_item">
				<text class="head_text">上传身份证国徽面</text>
				<view class="" style="margin-top: 40rpx;">
					<u-upload :fileList="fileList2" @afterRead="afterRead2" @delete="deletePic2" name="2"
						:maxCount="1"></u-upload>
				</view>
			</view>
			
			<!-- 毕业证书/学位证书照片-->
			<view class="info_item">
				<text class="head_text">上传毕业证书/学位证书照片</text>
				<view class="" style="margin-top: 40rpx;">
					<u-upload :fileList="fileList3" @afterRead="afterRead3" @delete="deletePic3" name="3"
						:maxCount="1"></u-upload>
				</view>
			</view>
			
			<view class="" style="width: 100%;height: 500rpx;">
				
			</view>

			<!-- 底部按钮 -->
			<view class="bottom">
				<button class="bottom_btn" style="background: #CCCCCC; width: 380rpx;" form-type="reset" @click="close">取消</button>
				<button class="bottom_btn" @click="handleSubmit"
					style="background: linear-gradient(0deg, #037CE3, #2EB3FF); margin-top: -88rpx;margin-left: 50%;"
					form-type="submit">确定</button>
			</view>

		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				member_id: '',
				company_id: '',
				editFormData: {
					name: '', //真实姓名
					id_number: '', //身份证号
					bank_number: '', //银行卡号
					id_card_url: '', //身份证人像面
					id_card_url2: '', //身份证国徽面
					diploma_url: '', //毕业证书
				},

				fileList1: [],
				fileList2: [],
				fileList3: [],
				
				qiniu_token:'',
				
				qiniuObj:''
			}
		},
		async onLoad() {
			this.token = uni.getStorageSync('btlh_process_token') || ''
			this.member_id = uni.getStorageSync('btlh_process_member_id') || ''
			this.company_id = uni.getStorageSync('bthl_process_LoginCompanyId') || ''
			this.getQiniuToken();
		},

		methods: {
			// 删除图片证书
			deletePic3(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.editFormData.diploma_url = '';
			},
			// 证书
			async afterRead3(event) {
				
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.editFormData.diploma_url = result;
					let item = this[`fileList${event.name}`][fileListLen]
					
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			
			// 删除图片国徽面
			deletePic2(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.editFormData.id_card_url2 = '';
			},
			// 国徽面
			async afterRead2(event) {
				
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				console.log('=======')
				let lists = [].concat(event.file)
				
				let fileListLen = this[`fileList${event.name}`].length
				
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.editFormData.id_card_url2 = result;
					let item = this[`fileList${event.name}`][fileListLen]
					
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			
			
			// 删除图片人像面
			deletePic1(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.editFormData.id_card_url = '';
			},
			// 人像面
			async afterRead1(event) {
				
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.editFormData.id_card_url = result;
					let item = this[`fileList${event.name}`][fileListLen]
					
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						/* url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						} */
						url: 'https://up-z2.qiniup.com',
						filePath: url,
						name: 'file',
						formData: {
							token: this.qiniu_token,
							key: url
						},
						success: uploadFileRes => {
							
							if(uploadFileRes.statusCode != 200){
								return uni.showToast({
									title:'上传七牛云出错',
									icon:'none'
								})
							}
							const result = JSON.parse(uploadFileRes.data).key;
							
							let UpFileItem = {
								type: 'pic',
								url: ''
							};
							UpFileItem.url = 'https://qiniu.sxpz.vip/' + result;
							
							resolve(UpFileItem.url)
						},
						fail: (res) => {
							console.log(res)
						}
					});
				})
			},

			// 获取七牛云token
			async getQiniuToken() {
				const {
					data: res
				} = await this.$myRequest({
					url: 'Qiniu/qiniuToken',
					method: 'GET',
					data: {
						member_id: this.member_id,
						token: this.token,
					}
				});
				this.qiniu_token = res;
				console.log(res)
			},

			// 点击确定
			async handleSubmit() {
				const {
					data: res
				} = await this.$myRequest({
					url: 'Index/entry',
					method: 'GET',
					data: {
						member_id: this.member_id,
						token: this.token,
						company_id: this.company_id,
						name: this.editFormData.name, //真实姓名
						id_number: this.editFormData.id_number, //身份证号
						bank_number: this.editFormData.bank_number, //银行卡号
						id_card_url: this.editFormData.id_card_url, //身份证人像面
						id_card_url2: this.editFormData.id_card_url2, //身份证国徽面
						diploma_url: this.editFormData.diploma_url, //毕业证书
					},

				})
				if (res.code != 200) return uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				})
				uni.showToast({
					title: res.datas.msg,
					icon: 'none'
				})

				uni.navigateBack({
					delta:1
				})
			},
			
			// 点击取消
			close(){
				uni.navigateBack({
					delta:1
				})
			},
		}
	}
</script>

<style>
	.bottom {
		width: 100%;
		height: 88rpx;
		position: fixed;
		bottom: 0;
	}

	.bottom_btn {
		width: 375rpx;
		height: 88rpx;
		margin-left: 0;
		border-radius: 0;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.texta {
		width: 690rpx;
		height: 305rpx;
		background: #F7F7F7;
		margin-top: 30rpx;

	}

	.info_item {
		width: 690rpx;
		height: auto;
		border-bottom: 1rpx solid #E3E3E3;
		margin-left: 30rpx;
		margin-top: 44rpx;
		position: relative;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.head_text {
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
	}
</style>
